<?php

declare(strict_types=1);

namespace Plugin\NEK\CodeGenerator\Command\Creater;

use Hyperf\Codec\Json;
use Hyperf\Command\Annotation\Command;
use Hyperf\Stringable\Str;
use Mine\AppStore\Command\AbstractCommand;
use Mine\AppStore\Plugin;
use Mine\AppStore\Command\CreateCommand;
use Mine\AppStore\Enums\PluginTypeEnum;

#[Command()]
class CreatePlugin extends CreateCommand
{
    protected const COMMAND_NAME = 'createPlugin';

    public function createNamespace(string $path, string $name): string
    {
        $pluginPath = Str::replace(Plugin::PLUGIN_PATH . '/', '', $path);
        $orgName = explode('/', $pluginPath);
        $namespace = 'Plugin\\';
        foreach ($orgName as $name) {
            $namespace .= Str::studly($name) . '\\';
        }
        return substr($namespace, 0, -1);
    }
    public function __invoke(): int
    {
        if (parent::__invoke() === AbstractCommand::SUCCESS) {
            $path = $this->input->getArgument('path');
            $name = $this->input->getOption('name');
            $namespace = $this->createNamespace($path, $name);
            $genMigration = "./swoole-cli -d swoole.use_shortname='Off' bin/hyperf.php gen:migration --path plugin/$path/Database/Migrations create_temp_table";
            $genSeeder = "./swoole-cli -d swoole.use_shortname='Off' bin/hyperf.php gen:seeder --path plugin/$path/Database/Seeders temp_seeder";
            exec($genMigration);
            // exec($genSeeder);
            $this->createLanguage($path);
            return AbstractCommand::SUCCESS;
        } else {
            return AbstractCommand::FAILURE;
        }
    }

    public function createMineJson(string $path, string $name, PluginTypeEnum $pluginType): void
    {
        $output = new \stdClass();
        $output->name = $name;
        $output->version = '1.0.0';
        $output->type = $pluginType->value;
        $output->description = $this->input->getOption('description') ?: 'This is a sample plugin';
        $author = $this->input->getOption('author') ?: 'demo';
        $output->author = [
            [
                'name' => $author,
            ],
        ];
        if ($pluginType === PluginTypeEnum::Backend || $pluginType === PluginTypeEnum::Mix) {
            $namespace = $this->createNamespace($path, $name) ?? 'Plugin\\' . ucwords(str_replace('/', '\\', Str::studly($name)));
            $this->createInstallScript($namespace, $path);
            $this->createUninstallScript($namespace, $path);
            $this->createConfigProvider($namespace, $path);
            $output->composer = [
                'require' => [],
                'psr-4' => [
                    $namespace . '\\' => 'src',
                ],
                'installScript' => $namespace . '\\' . 'InstallScript',
                'uninstallScript' => $namespace . '\\' . 'UninstallScript',
                'config' => $namespace . '\\' . 'ConfigProvider',
            ];
        }

        if ($pluginType === PluginTypeEnum::Mix || $pluginType === PluginTypeEnum::Frond) {
            $output->package = [
                'dependencies' => [],
            ];
        }
        $output = Json::encode($output);
        file_put_contents($path . '/mine.json', $output);
        $this->output->success(\sprintf('%s 创建成功', $path . '/mine.json'));
    }

    private function createLanguage(string $path): void
    {
        mkdir("plugin/$path/languages");
    }

    public function getStubDirectory(): string
    {
        return realpath(__DIR__) . '/stubs';
    }
}
